package com.XiaoLongGroup.webtech.controller;/**
 * @author : SLCcccc
 * @date : 2023/5/8 20:13
 */

import com.XiaoLongGroup.webtech.entity.User;
import com.XiaoLongGroup.webtech.service.ArticleService;
import com.XiaoLongGroup.webtech.service.UserService;

import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 *@ClassName RouterController
 *@Description TODO
 *@Author SLCcccc
 *@Date 2023/5/8 20:13
 *@Version 1.0
 */
@Controller
public class RouterController {
    /**
     * result
     */
    protected Map<String,Object > resultMap=new LinkedHashMap<>();
    @Resource
    private UserService userService;
    @Resource
    private ArticleService articleService;

    /**
     * 刷新Session
     * @param session
     * @return
     */
    private User getLoginUserAndreFlashUserSession(HttpSession session){
        String userId = ((User) session.getAttribute("loginUser")).getUserId();
        User user = userService.getUserById(userId);
        session.setAttribute("loginUser", user);
        return user;
    }

    @RequestMapping("/login")
    public ModelAndView login(){
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    @RequestMapping("/homePage")
    public ModelAndView homePage(){
        ModelAndView modelAndView = new ModelAndView("homePage");
        return modelAndView;
    }


    @RequestMapping("/main")
    public ModelAndView index(HttpSession session,
                              HttpServletRequest request) throws Exception{
        User user = getLoginUserAndreFlashUserSession(session);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("main");
        modelAndView.addObject("loginUser",user);
        return modelAndView;
    }

    @RequestMapping("/welcome")
    public ModelAndView welcome(HttpSession session,
                              HttpServletRequest request) throws Exception{
        User user = getLoginUserAndreFlashUserSession(session);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("loginUser",JSONArray.toJSONString(user));
        return modelAndView;
    }

    @RequestMapping("/userTable")
    public ModelAndView userTable(HttpSession session,
                                HttpServletRequest request) throws Exception{
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("userTable");
        return modelAndView;
    }

    @RequestMapping(value = "/articleTable", method = RequestMethod.GET)
    public ModelAndView articleTable(String userId) throws Exception{
        System.out.println(userId);
        User user = userService.getUserById(userId);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("articleTable/articleTable");
        modelAndView.addObject("userEdit", JSONArray.toJSONString(user));
        return modelAndView;
    }

    @RequestMapping("/articleMain")
    public ModelAndView articleMain(HttpSession session,
                                    HttpServletRequest request) throws Exception{
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("articleMain");
        return modelAndView;
    }


    @RequestMapping(value = "/table/edit", method = RequestMethod.GET)
    public ModelAndView tableEdit(String userId){
        System.out.println(userId);
        User userEdit = userService.getUserById(userId);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("table/edit");
        modelAndView.addObject("userEdit", JSONArray.toJSONString(userEdit));
        return modelAndView;
    }

    @RequestMapping("/table/add")
    public ModelAndView tableAdd(HttpSession session,
                                HttpServletRequest request) throws Exception{
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("table/add");
        return modelAndView;
    }

    @RequestMapping("/user-password")
    public ModelAndView userPassword(HttpSession session,
                                    HttpServletRequest request) throws Exception{
        User user = getLoginUserAndreFlashUserSession(session);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("user-password");
        modelAndView.addObject("loginUser",JSONArray.toJSONString(user));
        return modelAndView;
    }

    @RequestMapping("/user-setting")
    public ModelAndView userSetting(HttpSession session,
                                     HttpServletRequest request) throws Exception{
        User user = getLoginUserAndreFlashUserSession(session);
        ModelAndView modelAndView;
        modelAndView = new ModelAndView("user-setting");
        modelAndView.addObject("loginUser",JSONArray.toJSONString(user));
        return modelAndView;
    }

    @RequestMapping(value = "/menu")
    @ResponseBody
    public Object menu(){
        String path = Objects.requireNonNull(this.getClass().getClassLoader().getResource("")).getPath();
        System.out.println(path);
        String filePath = path + "\\init.json";
        System.out.println(filePath);
        resultMap=readJsonFile(filePath);
        return resultMap;
    }

    public static Map<String, Object> readJsonFile(String fileName) {
        Gson gson = new Gson();
        String json = "";
        try {
            File file = new File(fileName);
            Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");
            int ch = 0;
            StringBuffer buffer = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                buffer.append((char) ch);
            }
            reader.close();
            json = buffer.toString();
            return gson.fromJson(json, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
//    @RequestMapping(value = "/main")
//    public ModelAndView main(HttpSession session,
//                             HttpServletRequest request){
//        ModelAndView modelAndView=new ModelAndView("main");
//        return modelAndView;
//    }

//    @RequestMapping(value = "/logonDo",method = RequestMethod.POST)
//    @ResponseBody
//    public Object logonDo(User user,ModelMap modelMap){
//        try{
//            if(userService.userLogon(user)){
//                resultMap.put("status",200);
//                resultMap.put("message","注册成功");
//            }else{
//                resultMap.put("status",500);
//                resultMap.put("message","用户名或邮箱已存在");
//            }
//        }catch(Exception e){
//            resultMap.put("status",406);
//            resultMap.put("message","系统出错");
//            System.out.println(e.getMessage());
//        }
//        return resultMap;
//    }
//
//    @RequestMapping(value = "/changeDo",method = RequestMethod.POST)
//    @ResponseBody
//    public Object changeDo(User user){
//        try{
//            if(userService.changePwd(user)){
//                resultMap.put("status",200);
//                resultMap.put("message","修改成功");
//            }else{
//                resultMap.put("status",500);
//                resultMap.put("message","用户名或邮箱错误");
//            }
//        }catch(Exception e){
//            resultMap.put("status",406);
//            resultMap.put("message","系统出错");
//            System.out.println(e.getMessage());
//        }
//        return resultMap;
//    }

}
